<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/9
 * Time: 下午2:21
 */

namespace app\model;


use extend\STATUS_CODE;

class Brand extends Base
{
    protected $pk = 'id';
    // 此处表名请务必设置为public
    public $table = 'brand';

    /**
     * @purpose 通过ID获取分类名
     * @param int $id
     * @return string
     */
    public function idToName (int $id): string
    {
        return $this
            ->where('id', $id)
            ->value('name');
    }
    /**
     * @purpose 创建品牌
     * @param string $name
     * @param string $english
     * @param string $pinyin
     * @param string $thumb
     * @return bool
     * @throws \Exception
     */
    public function addOne(
        string $name,
        string $english,
        string $pinyin,
        string $thumb
    ): bool
    {
        if (!$name) {
            exception(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少品牌名称'
            );
        }
        if (!$english) {
            exception(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少品牌名称'
            );
        }
        if (!$thumb) {
            exception(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少品牌名称'
            );
        }
        $data               = [];
        $data['name']       = $name;
        $data['english']    = $english;
        $data['pinyin']     = $pinyin;
        $data['thumb']      = $thumb;
        $data['sort']       = 0;
        return (bool) $this->data($data)->save();
    }

    /**
     * @purpose 通过ID删除品牌
     * @param int $id
     * @return bool
     */
    public function deleteById (int $id): bool
    {
        return (bool) self::destroy($id);
    }

    /**
     * @purpose 获取品牌列表
     * @param $where
     * @param int $page
     * @param int $size
     * @param string $order
     * @param string $fields
     * @return array
     */
    public function items (
        $where,
        $page   = 1,
        $size   = 50,
        $order  = 'sort asc,create_at desc',
        $fields = 'id,name,english,pinyin,thumb,sort') :array
    {
        $total = $this->total($where);
        $list  = $this
            ->where($where)
            ->page($page,$size)
            ->order($order)
            ->field($fields)
            ->select();
        return [
            'total' => $total,
            'list'  => $list
        ];
    }

    /**
     * @purpose 根据条件获取数据条数
     * @param array $where
     * @return int
     */
    public function total(array $where): int
    {
        return (int) $this
            ->where($where)
            ->count();
    }

    /**
     * @purpose 通过ID获取单挑信息
     * @param int $id
     * @param string $fields
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function infoById (
        int $id,
        $fields = 'id,name,english,pinyin,thumb,sort'
    ){
        return $this
            ->where('id', $id)
            ->field($fields)
            ->find();
    }
}